<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/15 11:44
 * Blog：www.myblogs.xyz
 */

namespace app\api\controller;


use app\common\model\ReadMessages;

class Messages extends Common
{
    // 我的消息
    public function into()
    {
        $this->require = ['p'];
        $this->verifyParam();
        $this->verifyToken();
        // 分页
        $limit = 10; // 截取数量
        $firstRow = 0; // 开始截取
        $nowPage = empty($this->data['p'])? 1 : $this->data['p']; // 当前页码
        if ($nowPage > 1){
            $firstRow = $limit*($nowPage-1);
        }
        $messages_model = new \app\common\model\Messages();
        $ids_array = $messages_model->getMessagesId(1, $this->data['member_id']);
        // 筛选信息
        $messages_where['messages_id'] = ['in', implode(',', $ids_array) . ''];
        $messages_model->orders = 'send_time desc,messages_id desc';
        $messages_model->fields = 'messages_id,title,contents,send_time,target_id';
        $list = $messages_model->getList($messages_where, $firstRow . ',' . $limit);
        if (empty($list)){
            $this->jsonResult();
        }
        $read_messages = new ReadMessages();
        if (is_array($list)){
            foreach ($list as $key=>$val) {
                // 获取是否已读
                if ($read_messages->isRead(1, $this->data['member_id'], $val['messages_id'])){
                    $list[$key]['is_read'] = 1;
                }else{
                    $list[$key]['is_read'] = 0;
                }
                $list[$key]['create_time'] = date('Y.m.d', $val['send_time']);
            }
        }
        $this->jsonResult($list);
    }

    // 消息详情
    public function details()
    {
        $this->require = ['messages_id'];
        $this->verifyParam();
        $this->verifyToken();
        $messages_model = new \app\common\model\Messages();
        $messages_model->fields = 'messages_id,title,contents,send_time,target_id';
        $messages_data = $messages_model->getFind(['target'=>1,'messages_id'=>$this->data['messages_id']]);
        if (empty($messages_data)){
            $this->jsonError('get_error');
        }
        // setting is read message
        $this->readMessage($this->data);
        $messages_data['create_time'] = date('Y.m.d H:i:s', $messages_data['send_time']);
        $this->jsonResult($messages_data);
    }

    // 消息已读
    public function readMessage($inside = false)
    {
        if (!$inside){
            $this->require = ['messages_id'];
            $this->verifyParam();
            $this->verifyToken();
        }else{
            $this->data = $inside;
        }
        $list_where['target'] = 1;
        $list_where['messages_id'] = $this->data['messages_id'];
        $list_where['member_id'] = $this->data['member_id'];
        $read_messages = new ReadMessages();
        if ($read_messages->exists($list_where)){
            $read_messages->where($list_where)->update(['is_read'=>1]);
        }else{
            $list_where['is_read'] = 1;
            $read_messages->insert($list_where);
        }
        if (!$inside){
            $this->jsonSuccess();
        }
    }

    // 删除消息
    public function deleteMessage()
    {
        $this->require = ['messages_id'];
        $this->verifyParam();
        $this->verifyToken();
        // 删除消息处理
        $messages_model = new \app\common\model\Messages();
        $messages_data = $messages_model->getFind(['target'=>1,'messages_id'=>$this->data['messages_id']], 'messages_id,target_id');
        if (empty($messages_data)){
            $this->jsonError('delete_fail');
        }
        if ($messages_data['target_id'] != $this->data['member_id']){
            $list_where['target'] = 1;
            $list_where['messages_id'] = $this->data['messages_id'];
            $list_where['member_id'] = $this->data['member_id'];
            $read_messages = new ReadMessages();
            if ($read_messages->exists($list_where)){
                $rows = $read_messages->where($list_where)->update(['is_del'=>1]);
                if (!$rows){
                    $this->jsonError('删除失败');
                }
            }else{
                $list_where['is_del'] = 1;
                $read_messages->insert($list_where);
            }
        }else{
            $rows = $messages_model->where('messages_id', $this->data['messages_id'])->delete();
            if (!$rows){
                $this->jsonError('删除失败');
            }
        }
        $this->jsonSuccess('删除成功');
    }

    // 未读消息数量
    public function unreadNumber()
    {
        $this->require = ['member_id'];
        $this->verifyParam();
        if (empty($this->data['member_id'])){
            $data['unread_number'] = 0;
            $this->jsonResult($data);
        }
        // get unread message number
        $messages_model = new \app\common\model\Messages();
        $ids_array = $messages_model->getMessagesId(1, $this->data['member_id'], true);
        $data['unread_number'] = count($ids_array);
        $this->jsonResult($data);
    }
}